<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="addDepartment(company)"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- children:决定了当前节点有没有子级，label:展示当前节点的内容 -->
        <!-- props的作用： 把不是children和label的属性 映射成label和children -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handleCommand">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <!-- @click.native="del" -->
                        <el-dropdown-item
                          :command="{ type: 'add', data: data }"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          :command="{ type: 'edit', data: data }"
                        >修改部门</el-dropdown-item>
                        <el-dropdown-item
                          :command="{ type: 'del', id: data.id }"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 如果父组件想让子组件修改父组件中传过去的值，就必须把这个值放权给子组件 -->
    <!-- 父传子的时候 使用sync修饰符 表示父组件向子组件放权了 -->
    <!-- 子组件修改放权过来的值 要使用固定语法：this.$emit('update:Props中的属性',要修改的新值) -->
    <addDept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :node-data="nodeData"
      :depart-list="departList"
    />
  </div>
</template>

<script>
import { getDepartmentListApi, delDepartmentByIdApi } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
import addDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    addDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name'
      },
      company: {},
      showDialog: false,
      nodeData: {},
      departList: [],
      loading: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    /**
     * 获取部门列表
     */
    async getDepartmentList() {
      this.loading = true
      const res = await getDepartmentListApi()
      this.departList = res.data.depts
      this.departs = transListToTreeData(res.data.depts, '')
      this.company.name = res.data.companyName
      this.company.id = ''
      this.company.children = this.departs
      this.loading = false
    },
    /**
     * 给按钮绑定事件
     */
    handleCommand({ type, id, data }) {
      // console.log(type)
      if (type === 'add') {
        this.addDepartment(data)
      }
      if (type === 'edit') {
        this.editDepartment(data)
      }
      if (type === 'del') {
        this.delDepartmentById(id)
      }
    },
    /**
     * 修改
     */
    editDepartment(data) {
      this.showDialog = true
      this.nodeData = data
      // 调用子组件的回显方法
      this.$nextTick(() => {
        this.$refs.addDept.getDepartmentDetail()
        // this.$refs.addDept.showTitle = '修改部门'
      })
    },
    /**
     * 添加
     */
    addDepartment(data) {
      this.nodeData = data
      this.showDialog = true
      // this.$refs.addDept.getEmployeesSimle()
      // this.$refs.addDept.showTitle = '添加部门'
    },
    /**
     * 删除部门
     */
    delDepartmentById(id) {
      this.$confirm('亲！您确定删除吗？此操作不可逆', '温馨提示')
        .then(async() => {
          await delDepartmentByIdApi(id)
          this.$message.success('亲！删除成功了')
          this.getDepartmentList()
        })
        .catch(() => {})
    },
    closeDialogFn() {
      this.showDialog = false
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size: 14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url('~@/assets/common/add-circle.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url('~@/assets/common/minus-circle.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url('~@/assets/common/user-filling.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
